
/******************************************************************************
*@file  : app.c
*@brief : application
******************************************************************************/

#include "app.h"   


static void Enter_LowPowerMode(uint8_t mode);

/******************************************************************************
*@brief : app test
*         Press the key to switch the status of the lamp.
*@param : none
*@return: none
******************************************************************************/
void APP_Test(void)
{
    printfS("EXTI WakeupFromStop Demo\r\n\r\n");
    
	BSP_LED_Init();
	
	BSP_LED_On();
    HAL_Delay(500);
	BSP_LED_Off();
    HAL_Delay(500);
	BSP_LED_On();
    HAL_Delay(500);
	BSP_LED_Off();
    HAL_Delay(500);
	BSP_LED_On();
    
    BSP_PB_Init();
	
    while(1)
    {
        printfS("mcu enter stop mode!\r\n"); 
        printfS("Press the key (PB0:HIGH->GND) to exit stop mode!\r\n"); 
        
        Enter_LowPowerMode(PMU_STOP_INT_MODE);
        
		printfS("mcu has exited stop mode!\r\n");
		printfS("\r\n");
        
        HAL_Delay(500);
    };
}

/******************************************************************************
*@brief : app test
*@param : none
*@return: none
******************************************************************************/
void HAL_EXTI_LineCallback(uint32_t Line)
{
	if (Line == BSP_PB_PIN)
	{
		BSP_LED_Toggle();
	}
}


/******************************************************************************
* @brief : Low power test.
* @param : mode: Low power consumption mode selection
* @return: none
******************************************************************************/
static void Enter_LowPowerMode(uint8_t mode)
{
    switch(mode)
    {
        /* sleepģʽ */
        case PMU_SLEEP_INT_MODE:
            
            __set_PRIMASK(1);    // disable interrupt 
            HAL_SuspendTick();                
            HAL_PMU_EnterSleep(WAIT_FOR_INT); 
            __set_PRIMASK(0);    // enable interrupt 
            HAL_ResumeTick();
            break;
        
        case PMU_SLEEP_EVENT_MODE:
            
            __set_PRIMASK(1);    // disable interrupt 
            HAL_SuspendTick();                
            HAL_PMU_EnterSleep(WAIT_FOR_EVENT);  
            __set_PRIMASK(0);    // enable interrupt 
            HAL_ResumeTick();
            break;
        
        /* stopģʽ */
        case PMU_STOP_INT_MODE:
            
            __set_PRIMASK(1);    // disable interrupt 
            HAL_SuspendTick();
            HAL_PMU_EnterStop(WAIT_FOR_INT);
            __set_PRIMASK(0);    // enable interrupt 
            HAL_ResumeTick();
            break;
        
        case PMU_STOP_EVENT_MODE:
            
            __set_PRIMASK(1);    // disable interrupt 
            HAL_SuspendTick();
            HAL_PMU_EnterStop(WAIT_FOR_EVENT);
            __set_PRIMASK(0);    // enable interrupt 
            HAL_ResumeTick();
            break;
        
        default : 
            break;
    }
}

